package com.amat0urheu.service.impl;

import com.amat0urheu.dao.UserMapper;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.pojo.User;
import com.amat0urheu.service.UserService;
import com.amat0urheu.util.CommonUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import java.util.Iterator;
import java.util.List;



@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    final UserMapper userMapper;

    @Override
    public User getByUserName(String username) {
        return userMapper.selectByUserName(username);
    }

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {

        this.userMapper = userMapper;
    }

    @Override
    public boolean usernameIsRepeat(String username) {
        return userMapper.selectByUserName(username) != null;
    }

    @Override
    public int getIdByUserName(String username) {
        return userMapper.selectIdByUserName(username);
    }

    @Override
    public PageInfo<User> getAllUserByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectAllUser();
        if (list != null) return new PageInfo<>(list);
        return null;
    }

    @Override
    public ReturnRes searchUserByPage(String key, String value, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        String[] whiteList = new String[]{"uid", "uname", "skill", "unit", "role"};
        if (CommonUtil.strArrayIsHave(whiteList, key)) {
            List<User> list = userMapper.selectSearchUser(key, "%" + value + "%");
            if (list != null) return new ReturnRes(true, new PageInfo<>(list), "查询成功");
            return new ReturnRes(false, "查询失败");
        } else {
            return new ReturnRes(false, "师傅请勿尝试不安全的参数");
        }

    }

    @Override
    public void updateScoreByUid(int uid, int score) {
        userMapper.updateScoreByuid(uid,score);
    }

    @Override
    public ReturnRes addSome(MultipartFile file) throws IOException {


        if (file.isEmpty()) {
            System.out.println("请选择一个Excel文件进行上传。");
        }
        Workbook workbook = new XSSFWorkbook(file.getInputStream()); // 使用XSSFWorkbook处理.xlsx文件

        Sheet sheet = workbook.getSheetAt(0); // 假设文件的第一个Sheet包含用户信息

        Iterator<Row> rowIterator = sheet.iterator();

       while (rowIterator.hasNext()) {
           Row row = rowIterator.next();
            if (row.getRowNum() == 0) {
               continue; // 跳过标题行
            }

        }

            return new ReturnRes(true, "添加成功");

    }

    @Override
    public void checkIfSuperUser(String name) {
        userMapper.finSuperUserByUsename(name);
        System.out.println( userMapper.finSuperUserByUsename(name));
    }

    @Override
    public ReturnRes updatePassWord(String oldPassword, String newPassword,String user) {

        String md5OldPassword=new BCryptPasswordEncoder().encode(oldPassword);
        if(!md5OldPassword.equals(userMapper.getPasswordByuserName(user))){
            return new ReturnRes(false, "原始密码不正确");
        }
        String md5NewPassword=new BCryptPasswordEncoder().encode(newPassword);
       int count= userMapper.updatePasswordByuserName(user,md5NewPassword);
       if(count>0){
           return new ReturnRes(true, "修改成功");
       }
            return new ReturnRes(false, "修改成功");
    }


}
